// pages/wallet/wallet.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    totlePrice: '',
    price: "",
    index: 0,
    isShow: false,
    priceLength: 1,
    bankCardList: [],
    bankCardIndex: 0, //选择银行卡列表的索引值
    remainAmount: 0, //余额
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this;
    _this.abs(_this.data.price)
    _this.queryBankCard();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    var thirdSession = wx.getStorageSync('third_Session');
    if (thirdSession) {
    _this.queryUserMoney();
      _this.queryBankCard();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },
  //价格格式化
  abs(val) {
    //金额转换 分->元 保留2位小数 并每隔3位用逗号分开 1,234.56
    var str = (val / 100).toFixed(2) + '';
    console.log(str)
    var intSum = str.substring(0, str.indexOf(".")).replace(/\B(?=(?:\d{3})+$)/g, ',');//取到整数部分
    var dot = str.substring(str.length, str.indexOf("."))//取到小数部分搜索
    var ret = intSum + dot;
    return ret;
  },



  //获取价格
  gertPrice(e){
    let _this = this;
    //console.log(e.detail.value)
    let spot = e.detail.value.split(".");
    //console.log(spot)
    if (spot.length > 1){
      console.log()
      if (spot[1] != '' && spot[1].length > 2){
        //console.log(111)
        return;
      }else{
        if (spot[1].length == 2){
          _this.setData({
            price: e.detail.value,
            priceLength: e.detail.value.length,
          })
        } else{
          _this.setData({
            price: e.detail.value,
            priceLength: e.detail.value.length + 1,
          })
        }
        
      }
    }else{
      _this.setData({
        price: e.detail.value,
        priceLength: e.detail.value.length + 1,
      })
    }
    //console.log(_this.data.price)
  },


  //查询全部银行卡
  queryBankCard(){
    let _this = this;
    Api.queryBankCard().then( res => {
      console.log(res);
      if (res.data.length > 0) {
        let list = res.data;
        list.forEach(function (item, index, arr) {
          item.bankCardNoSubstr = item.bankCardNo.substr(item.bankCardNo.length - 4);
          //item.select = false;
        })
       // list[_this.data.bankCardIndex].select = true,
        _this.setData({
          bankCardList: list
        })
        console.log(_this.data.bankCardList)
      }
      
    })
  },
  
  show(){
    let _this = this;
    _this.setData({
      isShow: true,
    })
  },
  colse(){
    let _this = this;
    _this.setData({
      isShow: false,
    })
  },

  selectBank(e){
    let _this = this;
    let index = e.currentTarget.dataset.index;
    //console.log(index);
    _this.setData({
      bankCardIndex: index,
      isShow: false,
    })
  },

  //查询用户的余额信息
  queryUserMoney(){
    let _this = this;
    Api.queryUserMoney().then( res => {
      console.log(res)
      let remainAmount = res.data.remainAmount*100;
      _this.setData({
        remainAmount: _this.abs(remainAmount)
      })
    })
  },

  //提交提现申请
  txBtn(){
    let _this = this;
    let price = _this.data.price*100;
    let remainAmount = _this.data.remainAmount*100;
    if (price > remainAmount){
      wx.showToast({
        title: '不能大于余额',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (_this.data.bankCardList.length <= 0){
      wx.navigateTo({
        url: '/pages/bankCard/addBankCard',
      })
      return;
    }
    let data = {
      "amount": _this.data.price,
      "bankId": _this.data.bankCardList[_this.data.bankCardIndex].id,
      "userType": "MEMBER"
    }
    Api.cashOut(data).then( res => {
      console.log(res)
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 2000
      });
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        });
      }, 2000)
    })
  }

})